import os from "os";
import { app, shell } from "electron";
import type { MenuItemConstructorOptions } from "electron";
import { mainWindow } from "../electron-main";

/**
 * 常变量
 */
const platform = process.platform || os.platform();
const isMac = platform === "darwin";

/**
 * TS类型原因，拆解了菜单三木运算符类型推断：https://www.electronjs.org/zh/docs/latest/api/menu
 * 将所有条件判断的菜单片段预先定义，并给予明确的类型
 */
const appMenu: MenuItemConstructorOptions[] = isMac
  ? [
      {
        label: app.name,
        submenu: [
          { role: "about", label: "关于" },
          { type: "separator" },
          { role: "services", label: "服务" },
          { type: "separator" },
          { role: "hide", label: "隐藏" },
          { role: "hideOthers", label: "隐藏其他" },
          { role: "unhide", label: "全部显示" },
          { type: "separator" },
          { role: "quit", label: "退出" },
        ],
      },
    ]
  : [];

const fileMenuSub: MenuItemConstructorOptions[] = isMac
  ? [{ role: "close", label: "关闭窗口" }]
  : [{ role: "quit", label: "退出" }];

const editMenuSub: MenuItemConstructorOptions[] = isMac
  ? [
      { role: "pasteAndMatchStyle", label: "粘贴并匹配样式" },
      { role: "delete", label: "删除" },
      { role: "selectAll", label: "全选" },
      { type: "separator" },
      {
        label: "Speech",
        submenu: [
          { role: "startSpeaking", label: "开始朗读" },
          { role: "stopSpeaking", label: "停止朗读" },
        ],
      },
    ]
  : [
      { role: "delete", label: "删除" },
      { type: "separator" },
      { role: "selectAll", label: "全选" },
    ];

const windowMenuSub: MenuItemConstructorOptions[] = isMac
  ? [
      { type: "separator" },
      { role: "front", label: "前置全部窗口" },
      { type: "separator" },
      { role: "window", label: "窗口" },
    ]
  : [{ role: "close", label: "关闭" }];

// 2. 构建最终的模板，现在结构变得非常清晰，且类型完全正确
export const menuTemplate: MenuItemConstructorOptions[] = [
  // { role: 'appMenu' }
  ...appMenu,
  // 新增设置
  {
    label: "系统",
    submenu: [
      {
        label: "设置",
        accelerator: "Ctrl + K", // 快捷键
        click() {
          mainWindow?.webContents.send("show-settings");
          // console.log("yangzhide");
        },
      },
    ],
  },
  // { role: 'fileMenu' }
  {
    label: "文件",
    submenu: fileMenuSub,
  },
  // { role: 'editMenu' }
  {
    label: "编辑",
    submenu: [
      { role: "undo", label: "撤销" },
      { role: "redo", label: "重做" },
      { type: "separator" },
      { role: "cut", label: "剪切" },
      { role: "copy", label: "复制" },
      { role: "paste", label: "粘贴" },
      ...editMenuSub,
    ],
  },
  // { role: 'viewMenu' }
  {
    label: "视图",
    submenu: [
      { role: "reload", label: "重新加载" },
      { role: "forceReload", label: "强制重新加载" },
      { role: "toggleDevTools", label: "切换开发者工具" },
      { type: "separator" },
      { role: "resetZoom", label: "重置缩放" },
      { role: "zoomIn", label: "放大" },
      { role: "zoomOut", label: "缩小" },
      { type: "separator" },
      { role: "togglefullscreen", label: "切换全屏" },
    ],
  },
  // { role: 'windowMenu' }
  {
    label: "窗口",
    submenu: [
      { role: "minimize", label: "最小化" },
      { role: "zoom", label: "缩放" },
      ...windowMenuSub,
    ],
  },
  // { role: 'helpMenu' }
  {
    label: "帮助",
    role: "help",
    submenu: [
      {
        label: "了解更多",
        click: async () => {
          await shell.openExternal("https://electronjs.org");
        },
      },
    ],
  },
];
